package com.cloudsigns.factorymanager2.engineering.model;

import android.text.TextUtils;

import com.cloudsigns.factorymanager2.MyApplicaton;
import com.cloudsigns.factorymanager2.cusenum.CommonEnum;
import com.cloudsigns.factorymanager2.engineering.bean.MediaBean;
import com.cloudsigns.factorymanager2.engineering.bean.PublicationOrderDetailBean;
import com.cloudsigns.factorymanager2.engineering.utils.EngineeringUtil;
import com.cloudsigns.factorymanager2.project.utils.ConstantUtil;
import com.cloudsigns.factorymanager2.project.utils.ProjectUtil;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by tony on 2018/5/17.
 */

public class PublicationOrderAssignReceiveDetailModel implements IPublicationOrderAssignReceiveDetailModel {

    @Override
    public void getPublicationOrderAssignReceiveDetail(final String orderId, final OnPublicationOrderAssignReceiveDetailListener publicationOrderAssignReceiveDetailListener) {
        new Thread(new Runnable() {
            @Override
            public void run() {

                try {
                    HttpClient client = new DefaultHttpClient();
                    HttpPost httpPost = new HttpPost(ProjectUtil.PUBLICATION_ORDER_ASSIGN_RECEIVE_DETAIL);
                    // 添加http头信息
                    httpPost.addHeader(HTTP.CONTENT_TYPE, ConstantUtil.CONTENT_TYPE);
                    httpPost.addHeader("token", MyApplicaton.getApplication().getToken());
                    HttpResponse response;
                    JSONObject jsonObject = new JSONObject();
                    jsonObject.put("orderId", orderId);

                    StringEntity se = new StringEntity(jsonObject.toString(), HTTP.UTF_8);
                    httpPost.setEntity(se);
                    response = client.execute(httpPost);
                    String result = EntityUtils.toString(response.getEntity(), "UTF-8").trim();
                    JSONObject json = new JSONObject(result);
                    String code = json.getString("code");

                    if (null != publicationOrderAssignReceiveDetailListener) {
                        if (code.equals(EngineeringUtil.HTTP_SUCCESSED_COD)) {
                            JSONObject dataJsonObj = new JSONObject(json.getString("data"));
                            PublicationOrderDetailBean orderBean = new PublicationOrderDetailBean();
                            orderBean.setAssignCompleteAccountId(dataJsonObj.getString("assignCompleteAccountId"));
                            orderBean.setAssignReceiveAccountName(dataJsonObj.getString("assignCompleteAccountName"));


                            String assignCompleteOrderTime = dataJsonObj.getString("assignCompleteOrderTime");
                            if (!TextUtils.isEmpty(assignCompleteOrderTime) && !assignCompleteOrderTime.equals("null")) {
                                orderBean.setAssignCompleteOrderTime(Long.parseLong(assignCompleteOrderTime));
                            }

                            orderBean.setAssignReceiveAccountId(dataJsonObj.getString("assignReceiveAccountId"));
                            orderBean.setAssignReceiveAccountName(dataJsonObj.getString("assignReceiveAccountName"));
                            String assignReceiveOrderTime = dataJsonObj.getString("assignReceiveOrderTime");
                            if (!TextUtils.isEmpty(assignReceiveOrderTime) && !assignReceiveOrderTime.equals("null")) {
                                orderBean.setAssignReceiveOrderTime(Long.parseLong(assignReceiveOrderTime));
                            }
                            String assignStatusStr = dataJsonObj.getString("assignStatus");
                            if (!TextUtils.isEmpty(assignStatusStr)) {
                                if (assignStatusStr.equals("CREATED")) {
                                    orderBean.setAssignStatus(CommonEnum.AssignStatus.CREATED);
                                } else if (assignStatusStr.equals("EDITED")) {
                                    orderBean.setAssignStatus(CommonEnum.AssignStatus.EDITED);
                                } else if (assignStatusStr.equals("SEND")) {
                                    orderBean.setAssignStatus(CommonEnum.AssignStatus.SEND);
                                } else if (assignStatusStr.equals("RECEIVED")) {
                                    orderBean.setAssignStatus(CommonEnum.AssignStatus.RECEIVED);
                                } else if (assignStatusStr.equals("RETURNED")) {
                                    orderBean.setAssignStatus(CommonEnum.AssignStatus.RETURNED);
                                } else if (assignStatusStr.equals("COMPLETED")) {
                                    orderBean.setAssignStatus(CommonEnum.AssignStatus.COMPLETED);
                                }

                            }
                            String beginDate = dataJsonObj.getString("beginDate");
                            if (!TextUtils.isEmpty(beginDate) && !beginDate.equals("null")) {
                                orderBean.setBeginDate(Long.parseLong(beginDate));
                            }
                            orderBean.setCompanyId(dataJsonObj.getString("companyId"));
                            orderBean.setCompanyName(dataJsonObj.getString("companyName"));
                            String completeDate = dataJsonObj.getString("completeDate");
                            if (!TextUtils.isEmpty(completeDate) && !completeDate.equals("null")) {
                                orderBean.setCompleteDate(Long.parseLong(completeDate));
                            }
                            String completeStatusStr = dataJsonObj.getString("completeStatus");
                            if (!TextUtils.isEmpty(completeStatusStr)) {
                                if (completeStatusStr.equals("WORKING")) {
                                    orderBean.setCompleteStatus(CommonEnum.CompleteStatus.WORKING);
                                } else if (completeStatusStr.equals("WORKER_COMPLETE")) {
                                    orderBean.setCompleteStatus(CommonEnum.CompleteStatus.WORKER_COMPLETE);
                                } else if (completeStatusStr.equals("ADMIN_COMPLETE")) {
                                    orderBean.setCompleteStatus(CommonEnum.CompleteStatus.ADMIN_COMPLETE);
                                } else if (completeStatusStr.equals("SUPERADMIN_COMPLETE")) {
                                    orderBean.setCompleteStatus(CommonEnum.CompleteStatus.SUPERADMIN_COMPLETE);
                                }

                            }

                            String createDate = dataJsonObj.getString("createDate");
                            if (!TextUtils.isEmpty(createDate) && !createDate.equals("null")) {
                                orderBean.setCreateDate(Long.parseLong(createDate));
                            }
                            orderBean.setCreateOrderAccountId(dataJsonObj.getString("createOrderAccountId"));
                            orderBean.setCreateOrderAccountName(dataJsonObj.getString("createOrderAccountName"));
                            String finishDate = dataJsonObj.getString("finishDate");
                            if (!TextUtils.isEmpty(finishDate) && !finishDate.equals("null")) {
                                orderBean.setFinishDate(Long.parseLong(finishDate));
                            }
                            orderBean.setHasPauseOrder(dataJsonObj.getBoolean("hasPauseOrder"));
                            orderBean.setId(dataJsonObj.getString("id"));
                            orderBean.setIndustryCategory(dataJsonObj.getString("industryCategory"));

                            JSONArray mediaListJsonArr = dataJsonObj.getJSONArray("mediaList");
                            if (null != mediaListJsonArr) {
                                List<MediaBean> mediaBeanList = new ArrayList<MediaBean>();
                                for (int i = 0; i < mediaListJsonArr.length(); i++) {
                                    JSONObject mediaJsonObj = (JSONObject) mediaListJsonArr.get(i);
                                    MediaBean mediaBean = new MediaBean();

                                    String afterPicListArrStr = mediaJsonObj.getString("afterPicList");
                                    if (null != afterPicListArrStr && !afterPicListArrStr.equals("null") && !TextUtils.isEmpty(afterPicListArrStr)) {
                                        JSONArray afterPicListJsonArr = new JSONArray(afterPicListArrStr);

                                        List<String> afterPicList = new ArrayList<String>();
                                        for (int j = 0; j < afterPicListJsonArr.length(); j++) {
                                            afterPicList.add(afterPicListJsonArr.getString(i));
                                        }
                                        mediaBean.setAfterPicList(afterPicList);
                                    }

                                    mediaBean.setAreaName(mediaJsonObj.getString("areaName"));

                                    String beforePicListArrStr = mediaJsonObj.getString("beforePicList");
                                    if (null != beforePicListArrStr && !beforePicListArrStr.equals("null") && !TextUtils.isEmpty(beforePicListArrStr)) {
                                        JSONArray beforePicListJsonArr = new JSONArray(beforePicListArrStr);

                                        List<String> beforePicList = new ArrayList<String>();
                                        for (int j = 0; j < beforePicListJsonArr.length(); j++) {
                                            beforePicList.add(beforePicListJsonArr.getString(i));
                                        }
                                        mediaBean.setBeforePicList(beforePicList);
                                    }

                                    mediaBean.setFeedbackAccountId(mediaJsonObj.getString("feedbackAccountId"));
                                    mediaBean.setFeedbackAccountName(mediaJsonObj.getString("feedbackAccountName"));
                                    String feedbackDate = mediaJsonObj.getString("feedbackDate");
                                    if (!TextUtils.isEmpty(feedbackDate) && !feedbackDate.equals("null")) {
                                        orderBean.setFinishDate(Long.parseLong(feedbackDate));
                                    }


                                    String feedbackPicListStr = mediaJsonObj.getString("feedbackPicList");
                                    if (null != feedbackPicListStr && !feedbackPicListStr.equals("null") && !TextUtils.isEmpty(feedbackPicListStr)) {
                                        JSONArray feedbackPicListJsonArr = new JSONArray(feedbackPicListStr);

                                        List<String> feedbackPicList = new ArrayList<String>();
                                        for (int j = 0; j < feedbackPicListJsonArr.length(); j++) {
                                            feedbackPicList.add(feedbackPicListJsonArr.getString(i));
                                        }
                                        mediaBean.setFeedbackPicList(feedbackPicList);
                                    }


                                    String handleDate = mediaJsonObj.getString("handleDate");
                                    if (!TextUtils.isEmpty(handleDate) && !handleDate.equals("null")) {
                                        mediaBean.setHandleDate(Long.parseLong(handleDate));
                                    }

                                    mediaBean.setHandlerAccountId(mediaJsonObj.getString("handlerAccountId"));
                                    mediaBean.setHandlerAccountName(mediaJsonObj.getString("handlerAccountName"));
                                    mediaBean.setId(mediaJsonObj.getString("id"));
                                    mediaBean.setMediaName(mediaJsonObj.getString("mediaName"));
                                    mediaBean.setNumber(mediaJsonObj.getString("number"));


//                                    String positionXStr = mediaJsonObj.getString("positionX");
//                                    if (!TextUtils.isEmpty(positionXStr)) {
//                                        mediaBean.setPositionX(Double.parseDouble(positionXStr));
//                                    }
//
//                                    String positionYStr = mediaJsonObj.getString("positionY");
//                                    if (!TextUtils.isEmpty(positionYStr)) {
//                                        mediaBean.setPositionY(Double.parseDouble(positionYStr));
//                                    }
                                    mediaBean.setRegionId(mediaJsonObj.getString("regionId"));
                                    mediaBean.setRegionName(mediaJsonObj.getString("regionName"));

                                    String statusStr = mediaJsonObj.getString("status");
                                    if (!TextUtils.isEmpty(statusStr)) {
                                        if (statusStr.equals("PROCESSING")) {
                                            mediaBean.setStatus(CommonEnum.MediaStatus.PROCESSING);
                                        } else if (statusStr.equals("PROBLEM_APPEAR")) {
                                            mediaBean.setStatus(CommonEnum.MediaStatus.PROBLEM_APPEAR);
                                        } else if (statusStr.equals("PROBLEM_FEEDBACK")) {
                                            mediaBean.setStatus(CommonEnum.MediaStatus.PROBLEM_FEEDBACK);
                                        } else if (statusStr.equals("COMPLETE_FEEDBACK")) {
                                            mediaBean.setStatus(CommonEnum.MediaStatus.COMPLETE_FEEDBACK);
                                        } else if (statusStr.equals("COMPLETE")) {
                                            mediaBean.setStatus(CommonEnum.MediaStatus.COMPLETE);
                                        }

                                    }

                                    String typeStr = mediaJsonObj.getString("type");
                                    if (!TextUtils.isEmpty(typeStr)) {
                                        if (typeStr.equals("NORMAL")) {
                                            mediaBean.setType(CommonEnum.FeedBackType.NORMAL);
                                        } else if (typeStr.equals("UNUSUAL")) {
                                            mediaBean.setType(CommonEnum.FeedBackType.UNUSUAL);
                                        } else if (typeStr.equals("COMPLETED")) {
                                            mediaBean.setType(CommonEnum.FeedBackType.COMPLETED);
                                        }

                                    }

                                    mediaBean.setUnusualReason(mediaJsonObj.getString("unusualReason"));

                                    mediaBeanList.add(mediaBean);

                                }
                                orderBean.setMediaList(mediaBeanList);
                            }

                            String picListArrStr = dataJsonObj.getString("picList");
                            if (null != picListArrStr && !picListArrStr.equals("null") && !TextUtils.isEmpty(picListArrStr)) {
                                JSONArray picListJsonArr = new JSONArray(picListArrStr);

                                List<String> picList = new ArrayList<String>();
                                for (int j = 0; j < picListJsonArr.length(); j++) {
                                    picList.add(picListJsonArr.getString(j));
                                }
                                orderBean.setPicList(picList);
                            }

                            orderBean.setPublicationContent(dataJsonObj.getString("publicationContent"));
                            orderBean.setReceiveAccountId(dataJsonObj.getString("receiveAccountId"));
                            orderBean.setReceiveAccountName(dataJsonObj.getString("receiveAccountName"));
                            String receiveOrderTime = dataJsonObj.getString("receiveOrderTime");
                            if (!TextUtils.isEmpty(receiveOrderTime) && !receiveOrderTime.equals("null")) {
                                orderBean.setFinishDate(Long.parseLong(receiveOrderTime));
                            }

                            orderBean.setRegionId(dataJsonObj.getString("regionId"));
                            orderBean.setRegionName(dataJsonObj.getString("regionName"));
                            String sendOrderTime = dataJsonObj.getString("sendOrderTime");
                            if (!TextUtils.isEmpty(sendOrderTime) && !sendOrderTime.equals("null")) {
                                orderBean.setSendOrderTime(Long.parseLong(sendOrderTime));
                            }

                            String statusStr = dataJsonObj.getString("status");
                            if (!TextUtils.isEmpty(statusStr)) {
                                if (statusStr.equals("CREATED")) {
                                    orderBean.setStatus(CommonEnum.EngineeringOrderStatus.CREATED);
                                } else if (statusStr.equals("SEND")) {
                                    orderBean.setStatus(CommonEnum.EngineeringOrderStatus.SEND);
                                } else if (statusStr.equals("RECEIVED")) {
                                    orderBean.setStatus(CommonEnum.EngineeringOrderStatus.RECEIVED);
                                } else if (statusStr.equals("RETURNED")) {
                                    orderBean.setStatus(CommonEnum.EngineeringOrderStatus.RETURNED);
                                } else if (statusStr.equals("COMPLETED")) {
                                    orderBean.setStatus(CommonEnum.EngineeringOrderStatus.COMPLETED);
                                }
                            }
                            orderBean.setSuperAdminCompleteComment(dataJsonObj.getString("superAdminCompleteComment"));
                            orderBean.setWorkerLeaderCompleteComment(dataJsonObj.getString("workerLeaderCompleteComment"));

                            publicationOrderAssignReceiveDetailListener.getPublicationOrderAssignReceiveDetailSuccessed(orderBean);

                        } else {
                            String error_str = code;
                            error_str += "," + ConstantUtil.getErrorCode(code);
                            if (!json.isNull("error_str")) {
                                error_str += "," + json.getString("error_str");
                            }
                            publicationOrderAssignReceiveDetailListener.getPublicationOrderAssignReceiveDetailFailed(error_str);
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {

                }
            }
        }).start();
    }

    public interface OnPublicationOrderAssignReceiveDetailListener {
        void getPublicationOrderAssignReceiveDetailSuccessed(PublicationOrderDetailBean publicationOrderBean);

        void getPublicationOrderAssignReceiveDetailFailed(String desc);
    }
}
